package com.harry.utility;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.springframework.core.io.ClassPathResource;

import com.harry.basis.BasePoEntity;
import com.harry.decoupling.defi.proxy.GenericSrvProxy;

/**
 * 根据文本初始化数据库数据工具类
 */
public class InitializeUtil {

	public static void forEachLine(String resourceName, VoidFunc<String> each) throws Exception {
		ClassPathResource classPathResource = new ClassPathResource("initialization/" + resourceName);
		InputStream in = classPathResource.getInputStream();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
			String line = null;
			while (null != (line = reader.readLine())) {
				each.apply(line);
			}
		} finally {
			try {
				reader.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static interface VoidFunc<T> {

		void apply(T t) throws Exception;
	}

	public static <T extends BasePoEntity> T forceSave(T o) throws Exception {
		BeanCtxUtils.getBeanByType(GenericSrvProxy.class).save(o);
		return o;
	}
}
